package com.ruoyi.web.controller.erp;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.ProgressNodeDetail;
import com.ruoyi.system.domain.ProgressNodeMessage;
import com.ruoyi.system.domain.ProgressTemplateNode;
import com.ruoyi.system.mapper.ProgressNodeDetailMapper;
import com.ruoyi.system.mapper.ProgressNodeMessageMapper;
import com.ruoyi.system.mapper.ProgressTemplateNodeMapper;
import com.ruoyi.system.service.INodeMessageService;
import com.ruoyi.system.service.IProgressNodeDetailService;
import com.ruoyi.system.service.impl.ProgressNodeDetailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 进度节点明细Controller
 */
@RestController
@RequestMapping("/erp/progress/detail")
public class ProgressNodeDetailController extends BaseController {

    @Autowired
    private IProgressNodeDetailService progressNodeDetailService;

    @Autowired
    private INodeMessageService nodeMessageService;

    @Autowired
    private ProgressTemplateNodeMapper nodeMapper;


    /**
     * 查询节点明细
     */
    @GetMapping("/{orderNumber}/{nodeName}")
    public AjaxResult getNodeDetail(
        @PathVariable String orderNumber,
        @PathVariable String nodeName) {
        // 1. 根据订单编号和节点名称查找节点
        ProgressTemplateNode node = nodeMapper.selectNodeByOrderNumberAndName(orderNumber, nodeName);
        if (node == null) {
            return error("节点不存在");
        }

        // 2. 根据节点ID查询节点明细
        ProgressNodeDetail detail = progressNodeDetailService.selectNodeDetailByNodeId(node.getNodeId());
        if (detail == null) {
            return error("节点明细不存在");
        }

        return success(detail);
    }

    /**
     * 更新节点明细
     */
    @PutMapping("/{orderNumber}/{nodeName}")
    public AjaxResult updateDetail(
        @PathVariable String orderNumber,
        @PathVariable String nodeName,
        @RequestBody ProgressNodeDetail detail) {
        // 1. 根据订单编号和节点名称查找节点
        ProgressTemplateNode node = nodeMapper.selectNodeByOrderNumberAndName(orderNumber, nodeName);
        if (node == null) {
            return error("节点不存在");
        }

        // 2. 设置节点ID
        detail.setNodeId(node.getNodeId());

        // 3. 更新节点明细
        int rows = progressNodeDetailService.updateNodeDetail(detail);
        AjaxResult ajax = toAjax(rows);

        // 4. 获取最新的消息
        List<ProgressNodeMessage> messages = nodeMessageService.selectMessagesByNodeId(detail.getNodeId());
        if (!messages.isEmpty()) {
            ajax.put("message", messages.get(0).getMessage());
        }
        return ajax;
    }

    /**
     * 新增节点明细
     */
    @PostMapping("/{orderNumber}/{nodeName}")
    public AjaxResult addDetail(
        @PathVariable String orderNumber,
        @PathVariable String nodeName,
        @RequestBody ProgressNodeDetail detail) {
        // 1. 根据订单编号和节点名称查找节点
        ProgressTemplateNode node = nodeMapper.selectNodeByOrderNumberAndName(orderNumber, nodeName);
        if (node == null) {
            return error("节点不存在");
        }

        // 2. 设置节点ID
        detail.setNodeId(node.getNodeId());

        // 3. 新增节点明细
        int rows = progressNodeDetailService.insertNodeDetail(detail);
        AjaxResult ajax = toAjax(rows);

        // 4. 获取最新的消息
        List<ProgressNodeMessage> messages = nodeMessageService.selectMessagesByNodeId(detail.getNodeId());
        if (!messages.isEmpty()) {
            ajax.put("message", messages.get(0).getMessage());
        }
        return ajax;
    }
}
